<?

class Admin_Upgrade extends Controller {

    var $upgrade_path;

    var $recent_version;

    function Admin_Upgrade() {

        parent::Controller();

        $this->upgrade_path = array(
            array(
            'from'      =>  '0.9.21',
            'to'        =>  '0.9.30',
            'function'  =>  'u0921_to_0930'),
            array(
            'from'      =>  '0.9.30',
            'to'        =>  '0.9.31',
            'function'  =>  'u0930_to_0931'),
            array(
            'from'      =>  '0.9.31',
            'to'        =>  '0.9.32',
            'function'  =>  'u0931_to_0932'),
            array(
            'from'      =>  '0.9.32',
            'to'        =>  '0.9.33',
            'function'  =>  'u0932_to_0933'),
            array(
            'from'      =>  '0.9.33',
            'to'        =>  '0.9.34',
            'function'  =>  'u0933_to_0934'),
            array(
            'from'		=>	'0.9.34',
            'to'		=>	'0.9.35',
            'function'	=>	'u0934_to_0935')
        );

    }

    function index() {

        $this->load->model('mdl_version');

        foreach ($this->upgrade_path as $path) {

            $inktype_version = $this->mdl_version->get()->inktype_version;

            if ($path['from'] == $inktype_version) {

                if ($this->{$path['function']}()) {

                    echo 'Upgrade from ' . $path['from'] . ' to ' . $path['to'] . ' successful<br />';

                }

                else {

                    echo 'Upgrade from ' . $path['from'] . ' to ' . $path['to'] . ' FAILED. Script exiting.';

                    exit;

                }

            }

        }

        echo 'Upgrade complete.';

    }

    function u0921_to_0930() {

        $this->db->query("CREATE TABLE inktype_visitors (
	visitor_id int(11) NOT NULL DEFAULT 0 COMMENT '' auto_increment,
	site_id int(11) NOT NULL DEFAULT 0 COMMENT '',
	`date` varchar(25) NOT NULL DEFAULT '' COMMENT '' COLLATE utf8_general_ci,
	browser varchar(255) NOT NULL DEFAULT '' COMMENT '' COLLATE utf8_general_ci,
	platform varchar(255) NOT NULL DEFAULT '' COMMENT '' COLLATE utf8_general_ci,
	referrer longtext NOT NULL DEFAULT '' COMMENT '' COLLATE utf8_general_ci,
	current_url varchar(255) NOT NULL DEFAULT '' COMMENT '' COLLATE utf8_general_ci,
	ip_address varchar(255) NOT NULL DEFAULT '' COMMENT '' COLLATE utf8_general_ci,
	PRIMARY KEY (visitor_id),
	INDEX site_id (site_id)
	) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

        $this->db->query("ALTER TABLE inktype_comments
	MODIFY comment_date varchar(12) NULL DEFAULT NULL COMMENT '' COLLATE utf8_general_ci,
	DROP INDEX user_id_2;");

        $this->db->delete('inktype_version');

        $db_array = array(
            'inktype_version'	=>	'0.9.30',
            'version_date'	=>	strtotime('04/28/2009')
        );

        $this->db->insert('inktype_version', $db_array);

        return TRUE;

    }

    function u0930_to_0931() {

        $this->db->query("ALTER TABLE inktype_comments
	ADD ip_address varchar(15) NOT NULL DEFAULT '' COLLATE utf8_general_ci AFTER comment;");

        $this->db->query("ALTER TABLE inktype_visitors
	MODIFY ip_address varchar(15) NOT NULL DEFAULT '' COLLATE utf8_general_ci;");

        $db_array = array(
            'inktype_version'	=>	'0.9.31',
            'version_date'	=>	strtotime('05/07/2009')
        );

        $this->db->delete('inktype_version');

        $this->db->insert('inktype_version', $db_array);

        return TRUE;

    }

    function u0931_to_0932() {

        $this->db->query("ALTER TABLE inktype_comments
	ADD is_spam int(1) NOT NULL DEFAULT '0' AFTER ip_address,
	ADD INDEX is_spam (is_spam);");

        $db_array = array(
            'inktype_version'	=>	'0.9.32',
            'version_date'	=>	strtotime('05/07/2009')
        );

        $this->db->delete('inktype_version');

        $this->db->insert('inktype_version', $db_array);

        return TRUE;

    }

    function u0932_to_0933() {

        $db_array = array(
            'inktype_version'	=>	'0.9.33',
            'version_date'	=>	strtotime('05/12/2009')
        );

        $this->db->delete('inktype_version');

        $this->db->insert('inktype_version', $db_array);

        return TRUE;

    }

    function u0933_to_0934() {

        $db_array = array(
            'inktype_version'	=>	'0.9.34',
            'version_date'	=>	strtotime('05/17/2009')
        );

        $this->db->delete('inktype_version');

        $this->db->insert('inktype_version', $db_array);

        return TRUE;

    }
    
    function u0933_to_0934() {

        $db_array = array(
            'inktype_version'	=>	'0.9.35',
            'version_date'	=>	strtotime('07/24/2009')
        );

        $this->db->delete('inktype_version');

        $this->db->insert('inktype_version', $db_array);

        return TRUE;

    }

}

?>